﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace CommandLib
{
    /// <summary>
    /// 加密辅助类
    /// </summary>
    public sealed class Md5Helper
    {
        private const string md5key= "System.Security.Cryptography.MD5";
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <returns></returns>
        public static string MD5Encrypt(string input)
        {

            return MD5Encrypt(input, Encoding.UTF8);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <param name="encode">字符的编码</param>
        /// <returns></returns>
        public static string MD5Encrypt(string input, Encoding encode)
        {
            TripleDES.Create();
            var md5 = MD5.Create(md5key);
            byte[] t = md5.ComputeHash(encode.GetBytes(input));
            var sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            return sb.ToString();
        }
        /// <summary>
        /// 加密对比
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static bool MD5Contains(string str1, string str2)
        {
            var result = false;
            if (str1 == null || str2 == null)
            {
                return false;
            }
            else
            {
                var s1 = MD5Encrypt(str1);
                var s2 = MD5Encrypt(str2);
                result = s1.Equals(s2);
            }
            return result;

        }
    }
}
